import Vue from "vue";

import Vuex from "vuex";
Vue.use(Vuex);

import axios from "axios";


var store = new Vuex.Store({
    state: {
        data: {},
        recommend: [],
        registThen: {},
        registShow: false,
        loginThen: [],
        loginShow: false,
        userLogin: null,
        exitThen: {},
        sellerThen: null,
        sellerLogin: {},
        launchThen: null,
        launchDone: false,
        addThen: null,
        chooseThen: [],
        choose: [],
        chooseClassify: null,
        clickClassifyOneName: null,
        searchListThen: [],
        productThen: {},
        productThenImg: [],
        ManagerThen: null,
        addcartMsg: null,
        addCartShow: false,
        shoppingCartClassify: [],
        searchThen:null,

    },

    mutations: {
        setData: function (state, value) {
            state.data = value;
        },
        setRecommend: function (state, value) {
            state.recommend = value;
        },
        setRegist: function (state, value) {
            state.registThen = value;
            state.registShow = true;
        },
        setLogin: function (state, value) {
            state.loginThen = value;
            state.loginShow = true;
        },
        setLoginShow: function (state, value) {
            state.loginShow = false;
        },
        setIsLogin: function (state, value) {
            if (value.err == 0) {
                state.userLogin = value;
            } else {
                state.userLogin = null;
            }
        },
        setExit: function (state, value) {
            state.exitThen = value;
            state.userLogin = null;
        },
        setSeller: function (state, value) {
            state.sellerThen = value;
        },
        setIsSeller: function (state, value) {
            state.sellerLogin = value;
        },
        setLaunch: function (state, value) {
            state.launchThen = value;
            state.launchDone = true;
        },
        setAdd: function (state, value) {
            state.addThen = value;
        },
        setChoose: function (state, value) {
            state.chooseThen = value;
            state.chooseClassify = value.data[0].classifyTwoName;
            state.clickClassifyOneName = value.data[0].classifyOneName;
            state.choose = [];
            for (var i = 0; i < state.chooseThen.data.length; i++) {
                var arr = {};
                arr.name

                    = state.chooseThen.data[i].classifyOneName;
                arr.value = state.chooseThen.data[i].classifyOneName;
                arr.parent = 0;
                state.choose.push(arr);
                arr = {};
                for (var j = 0; j < state.chooseThen.data[i].classifyTwoName.length; j++) {
                    arr.name

                        = state.chooseThen.data[i].classifyTwoName[j];
                    arr.value = state.chooseThen.data[i].classifyTwoName[j];
                    arr.parent = state.chooseThen.data[i].classifyOneName;
                    state.choose.push(arr);
                    arr = {};
                }
            }
        },
        setLaunchDone: function (state, value) {

            state.launchDone = false;
        },
        setChooseClassify: function (state, value) {
            state.chooseClassify = value[0];
            state.clickClassifyOneName = value[1];
        },
        setSearchList: function (state, value) {
            state.searchListThen = value;
        },
        setProduct: function (state, value) {
            state.productThen = value.data;
            state.productThenImg = [];
            for (var i = 0; i < value.data.goodsOfImg.length; i++) {
                var arr = {};
                arr.url = "";
                arr.img = value.data.goodsOfImg[i];
                state.productThenImg.push(arr);
                arr = {};
            }
        },
        setManager: function (state, value) {
            state.ManagerThen = value;
        },
        serAddcart: function (state, value) {
            state.addcartMsg = value.msg;
            state.addCartShow = true;
        },
        setAddcartShow: function (state) {
            state.addCartShow = false;
        },
        setShoppingCart: function (state, value) {
            state.shoppingCartClassify = [];
            value.data.shoppingCart.forEach(function (e,i) {
                var arr = {};
                arr.name

                    = e.classifyTwoName;
                arr.goods = [];
                arr.isCheck = false;
                e.isCheckd = false;
                e.count = value.data.count[i];
                arr.goods.push(e);

                var isHas = false;
                state.shoppingCartClassify.forEach(function (v) {
                    if (v.name == arr.name
                    ) {
                        isHas = true;
                        e.isCheckd = false;
                        e.count = value.data.count[i];
                        v.goods.push(e);
                    }
                }, this);
                if (!isHas) {
                    state.shoppingCartClassify.push(arr);
                }
            }, this);
        },
        setGross:function(state,value){
            state.total = value.total;
            state.gross = value.gross;
        },
        setSearch:function(state,value){
            state.searchThen = value;
        }

    },
    actions: {
        requestData: function (state, data) {
            axios.get("/home")
                .then(res => {
                    // console.dir(res.data)
                    state.commit("setData", res.data);
                });
        },
        requestRecommend: function (state, data) {
            axios.get("/home/recommend")
                .then(res => {
                    state.commit("setRecommend", res.data)
                })
        },
        // 注册
        requestRegist: function (state, value) {
            axios.post("/users/regist", value)
                .then(res => {
                    state.commit("setRegist", res.data);
                    location.hash = "/login";
                });
        },
        // 登录
        requestLogin: function (state, value) {
            axios.post("/users/login", value)
                .then(res => {
                    state.commit("setLogin", res.data);
                    location.hash = "/";
                });
        },
        // 判断
        requestIsLogin: function (state) {
            axios.post("/users/islogin")
                .then(res => {
                    state.commit("setIsLogin", res.data);
                })
        },
        requestExit: function (state) {
            axios.post("/users/exit")
                .then(res => {
                    state.commit("setExit", res.data);
                })
        },
        // 卖家
        requestSeller: function (state, value) {
            axios.post("/users/seller", value)
                .then(res => {
                    state.commit("setSeller", res.data);
                });
        },
        // 判断是否是卖家
        requestIsSeller: function (state) {
            axios.post("/users/isseller")
                .then(res => {
                    state.commit("setIsSeller", res.data);
                });
        },
        // 发布
        requestLaunch: function (state, value) {
            axios.post("/home/launch", value)
                .then(res => {
                    state.commit("setLaunch", res.data);
                });
        },
        // 添加分类
        requestAdd: function (state, value) {
            axios.post("/home/add", value)
                .then(res => {
                    state.commit("setAdd", res.data);
                });
        },
        // 选择分类发布
        requestChoose: function (state) {
            axios.get("/home/choose")
                .then(res => {
                    state.commit("setChoose", res.data);
                });
        },
        // 查询商品
        requestSearchList: function (state, value) {
            axios.get("/home/searchList", { params: value })
                .then(res => {
                    state.commit("setSearchList", res.data);
                });
        },
        // 产品详情
        requestProduct: function (state, value) {
            axios.get("/home/product", { params: value })
                .then(res => {
                    state.commit("setProduct", res.data);
                });
        },
        requestManager: function (state, value) {
            axios.get("/home/manager", { params: value })
                .then(res => {
                    console.log(res.data)
                    state.commit("setManager", res.data);
                });

        },
        // 添加到购物车
        requstAddcart: function (state, value) {
            axios.post("/users/addcart", value)
                .then(res => {
                    console.log(res.data);
                    state.commit("serAddcart", res.data);
                })
        },
        requestShoppingCart: function (state) {
            axios.get("/users/shoppingCart")
                .then(res => {
                    state.commit("setShoppingCart", res.data)
                })
        },
        requestCount:function(state,value){
            axios.post("/users/count",value)
                .then(res => {
                    store.dispatch("requestShoppingCart");
                })
        },
        // 购物车删除选中
        requestRemoveShoppingcart:function(state,value){
            axios.get("/users/delete",{params:value})
                .then(res => {
                    store.dispatch("requestShoppingCart");
                })
        },
        // 输入框搜索商品
        requestScarch:function(state,value){
            axios.get("/home/search",{params:value})
                .then(res => {
                    state.commit("setSearch",res.data.data)
                    
                })
        }
    }
});


export default store; 